package com.weigee.ktv.v.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.l.d.app.library.util.ListUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.weigee.ktv.R;
import com.weigee.ktv.m.entity.TransactionHistoryEntity;
import com.weigee.ktv.m.retrofit.ApiRequest;
import com.weigee.ktv.v.adapter.rv.TransactionHistoryAdapter;
import com.weigee.ktv.v.view.WrapTitleBar;

import butterknife.BindView;

/**
 * Created by linde on 2017/04/26.
 * <p/>
 * 交易记录
 */
public class TransactionHistoryFragment extends BaseFragment implements ApiRequest.Back {
    @BindView(R.id.wrap_title_bar)
    WrapTitleBar mTitleBar;

    @BindView(R.id.t_refresh)
    TwinklingRefreshLayout m_tRefresh;
    @BindView(R.id.recycler_view)
    RecyclerView mRv;

    TransactionHistoryAdapter m_adp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.recycler_wrap, container, false);
    }

    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState) {
        mTitleBar.setTitle("交易记录");
        mTitleBar.setLeftButton(View.VISIBLE, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });

        ListUtil.initTwinklingRefresh(m_tRefresh, new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                request(false);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                request(true);
            }
        });
        m_adp = new TransactionHistoryAdapter();
        ListUtil.initRecyclerView(mRv, null, m_adp);

        request(false);
    }

    private void request(boolean isLoadMoreData) {
        ApiRequest.transactionHistory(m_adp, false, TransactionHistoryEntity.class, this);
    }

    @Override
    public void onCompletely(boolean append, boolean enableAppendContinue) {
        if (m_tRefresh != null) {
            if (append) {
                m_tRefresh.finishLoadmore();
            } else {
                m_tRefresh.finishRefreshing();
            }
            m_tRefresh.setEnableLoadmore(enableAppendContinue);
        }
    }
}
